package com.kanas.webak.config.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kanas.app.viewobject.PlainTree;
import com.kanas.framework.business.BusinessHandler;
import com.kanas.framework.business.ResponseMessage;
import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.config.domain.DictType;
import com.kanas.webak.config.domain.DictValue;
import com.kanas.webak.config.service.DictTypeService;
import com.kanas.webak.config.service.DictValueService;

@Controller
@RequestMapping("app/config/dd")
public class DictDictionaryController extends ControllerSupport {

	private DictTypeService dictTypeService;
	private DictValueService dictValueService;
	
	@RequestMapping(value="/ddtype/list.json",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> listAll(){
		Map<String,Object> response=new HashMap<String,Object>();
		response.put("identifier", "id");
		response.put("label", "name");
		List<DictType> ddTypes=dictTypeService.findAll();
		for(DictType ddt:ddTypes){
			ddt.setDictValue(null);
		}
		response.put("items", ddTypes);
		return response;
	}

	@RequestMapping(value="",method=RequestMethod.GET)
	public ModelAndView dd(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/dd");
		return mv;
	}
	
	@RequestMapping(value="/ddtype/new.html",method=RequestMethod.GET)
	public ModelAndView addNew(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/dict_type_new");
		return mv;
	}
	
	@RequestMapping(value="/ddvalue/{pid}/new.html",method=RequestMethod.GET)
	public ModelAndView addValueNew(@PathVariable("pid")String pId,@RequestParam(value="typeId",required=false)final String typeId){
		ModelAndView mv=new ModelAndView();
		mv.addObject("pid", pId);
		mv.addObject("typeId", typeId);
		mv.setViewName("modules/sysadmin/dict_value_new");
		return mv;
	}
	
	@RequestMapping(value="/ddRootvalue/new.html",method=RequestMethod.GET)
	public ModelAndView addValueNew(@RequestParam(value="typeId",required=false)final String typeId){
		ModelAndView mv=new ModelAndView();
		mv.addObject("pid", "root");
		mv.addObject("typeId", typeId);
		mv.setViewName("modules/sysadmin/dict_value_new");
		return mv;
	}
	
	@RequestMapping("/ddtype/new.do")
	public @ResponseBody ResponseMessage addValueNew(final DictType dict) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return dictTypeService.addNew(dict);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping("/ddvalue/{pid}/new.do")
	public @ResponseBody ResponseMessage addNew(final DictValue dict,@PathVariable("pid") final String pId,@RequestParam(value="typeId",required=false)final String typeId) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				if(!pId.equals("root")){
					dict.setParentId(pId);
				}
				DictType ddType=dictTypeService.findOne(typeId);
				dict.setDictType(ddType);
				return dictValueService.addNew(dict);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/ddtype/edit/{dictid}.html")
	public ModelAndView EditView(@PathVariable("dictid")String did){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/dict_type_edit");
		mv.addObject("dictType", dictTypeService.findOne(did));
		return mv;
	}
	
	@RequestMapping(value="/ddvalue/edit/{dictid}.html")
	public ModelAndView EditValueView(@PathVariable("dictid")String did){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/dict_value_edit");
		mv.addObject("dictValue", dictValueService.findOne(did));
		return mv;
	}
	
	@RequestMapping(value="/ddvalue/{dictTypeid}.html")
	public ModelAndView ddValueView(@PathVariable("dictTypeid")String did){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/dict_value_tree");
		DictType dictType=dictTypeService.findOne(did);
		mv.addObject("dictType", dictType);
		List<DictValue> dictValues=dictValueService.findByDictType(dictType);
		for(DictValue dv:dictValues){
			if(dv.getParentId()==null){
				dv.setParentId("root");
			}
		}
		mv.addObject("typeId",did);
		mv.addObject("dictValues",dictValues);
		return mv;
	}
	
	@RequestMapping(value="/ddValue/list.json",method=RequestMethod.GET)
	public @ResponseBody List<PlainTree> listDictValue(@RequestParam(value="typeId",required=false)final String typeId){
		DictType dictType=dictTypeService.findOne(typeId);
		List<DictValue> dictValues=dictValueService.findByDictType(dictType);
		List<PlainTree> response=new ArrayList<PlainTree>();
		PlainTree treeNode=new PlainTree("root",dictType.getLabel(),"");
		response.add(treeNode);
		for(DictValue dv:dictValues){
			if(dv.getParentId()==null){
				dv.setParentId("root");
			}
			treeNode=new PlainTree(dv.getId(),dv.getLabel(),dv.getParentId());
			response.add(treeNode);
		}
		return response;
	}	
	
	@RequestMapping("/ddtype/{dictid}/edit.do")
	public @ResponseBody ResponseMessage update(final DictType dict,@PathVariable("dictid")final String did) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				dict.setOldId(did);
				return dictTypeService.update(dict);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping("/ddvalue/{dictid}/edit.do")
	public @ResponseBody ResponseMessage updateValue(final DictValue dict,@PathVariable("dictid")final String did) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				dict.setOldId(did);
				return dictValueService.update(dict);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/ddtype/{dictid}",method=RequestMethod.DELETE)
	public @ResponseBody String delete(@PathVariable("dictid") final String did){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				DictType dictType=dictTypeService.findOne(did);
				List<DictValue> dictValues=dictValueService.findByDictType(dictType);
				dictValueService.removeAll(dictValues);
				String result=dictTypeService.remove(did);
				return result;
			}
		}, logger);
		return rm.toString();
	}
	
	@RequestMapping(value="/ddvalue/{dictid}",method=RequestMethod.DELETE)
	public @ResponseBody String deleteValue(@PathVariable("dictid") final String did){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return dictValueService.remove(did);
			}
		}, logger);
		return rm.toString();
	}
	
	public DictTypeService getDictTypeService() {
		return dictTypeService;
	}
	
	@Resource(name="com.kanas.webak.config.service.dicttypeservice")
	public void setDictTypeService(DictTypeService dictTypeService) {
		this.dictTypeService = dictTypeService;
	}

	public DictValueService getDictValueService() {
		return dictValueService;
	}
	
	@Resource(name="com.kanas.webak.config.service.dictvalueservice")
	public void setDictValueService(DictValueService dictValueService) {
		this.dictValueService = dictValueService;
	}
}
